<template>
  <el-dialog title="新增员工" :visible="showDialog" width="35%" @close="btnClose">
    <el-form ref="addEmployee" label-width="100px" :rules="rules" :model="fromData">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="fromData.username" placeholder="请输入姓名" style="width: 90%" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="fromData.mobile" placeholder="请输入手机号" style="width: 90%" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="fromData.timeOfEntry" placeholder="请选择入职日期" style="width: 90%" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="fromData.formOfEmployment" placeholder="请选择" style="width: 90%">
          <el-option v-for="item in employeesEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="fromData.workNumber" placeholder="请输入工号" style="width: 90%" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="fromData.departmentName" placeholder="请输入部门" style="width: 90%" @focus="getDepartments" />
        <!-- 放置一个树形组件 -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="fromData.correctionTime" placeholder="请选择转正日期" style="width: 90%" />
      </el-form-item>
      <!-- 确定和取消 -->
    </el-form>
    <el-row type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnClose">取 消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确 定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import employeesEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表单数据
      fromData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      },
      treeData: [],
      showTree: false,
      loading: false,
      employeesEnum // 聘用形式数据
    }
  },
  methods: {
    // 点击确认按钮
    async btnOK() {
      try {
        await this.$refs.addEmployee.validate()
        // 校验成功
        await addEmployee(this.fromData) // 提交数据
        this.$parent.getEmployeeList() // 父组件的实例
        this.$parent.showDialog = false // 关闭弹层
      } catch (error) {
        console.log(error)
      }
    },
    // 取消按钮
    btnClose() {
      // 删除数据
      this.fromData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$emit('update:showDialog', false)
      this.$refs.addEmployee.clearValidate()
    },
    // 获取组织架构
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 组织架构节点被点击
    selectNode(node) {
      this.fromData.departmentName = node.name
      this.showTree = false
    }
  }
}
</script>

<style>

</style>
